<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>过滤桶 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_filter_bucket.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_filter_bucket.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_filter_bucket.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_filter_bucket.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="_filtering_queries_and_aggregations.html">过滤和聚合</a></span>
»
<span class="breadcrumb-node">过滤桶</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_filtering_queries.html">« 过滤</a>
</span>
<span class="next">
<a href="_post_filter.html">后过滤器 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_filter_bucket"></a>过滤桶 (Filter Bucket)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/300_Aggregations/45_filtering.asciidoc">edit</a>
</h2>
</div></div></div>
<p>但是如果我们只想对聚合结果过滤怎么办？ 假设我们正在为汽车经销商创建一个搜索页面，
我们希望显示用户搜索的结果，但是我们同时也想在页面上提供更丰富的信息，包括（与搜索匹配的）上个月度汽车的平均售价。</p>

<p>这里我们无法简单的做范围限定，因为有两个不同的条件。搜索结果必须是 <code class="word">ford</code> ，但是聚合结果必须是 <code class="word">ford</code> 且 销售时间是在一个月前(<code class="word">sold &gt; now - 1M</code>) 。</p>
<p>为了解决这个问题，我们可以用一种特殊的桶，叫做 <code class="literal">filter</code> （过滤桶） 。 我们可以指定一个过滤桶，当文档满足过滤桶的条件时，我们将其加入到桶内。</p>
<p>查询结果如下：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /cars/transactions/_search
{
   "size" : 0,
   "query":{
      "match": {
         "make": "ford"
      }
   },
   "aggs":{
      "recent_sales": {
         "filter": { <a id="CO197-1"></a><i class="conum" data-value="1"></i>
            "range": {
               "sold": {
                  "from": "now-1M"
               }
            }
         },
         "aggs": {
            "average_price":{
               "avg": {
                  "field": "price" <a id="CO197-2"></a><i class="conum" data-value="2"></i>
               }
            }
         }
      }
   }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/300_Aggregations/45_filtering.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO197-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>使用 <code class="literal">filter (过滤)</code> 桶在 <code class="literal">查询</code> 范围基础上应用过滤器。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO197-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="word">avg</code> 度量只会对 <code class="word">ford</code> 和上个月售出的文档计算平均售价。</p>
</td>
</tr>
</table>
</div>
<p>因为 <code class="literal">filter</code> 桶和其他桶的操作方式一样，所以可以随意将其他桶和度量嵌入其中。所有嵌套的组件都会 "继承" 这个过滤，这使我们可以按需针对聚合过滤出选择部分。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="_filtering_queries.html">« 过滤</a>
</span>
<span class="next">
<a href="_post_filter.html">后过滤器 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>